package com.chejianer.push.config;


import com.chejianer.push.service.impl.ServletContextListenerImpl;
import com.github.ziplet.filter.compression.CompressingFilter;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

public class TheWebApplicationInitializer  extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {

        return new Class[] {AppConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {

        return new Class[] {WebMvcConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }

//    @Override
//    protected Filter[] getServletFilters() {
//
//        Filter encodingFilter = new CharacterEncodingFilter("UTF-8");
//        encodingFilter.
//        Filter[] filters = new Filter[]{
//            new CharacterEncodingFilter()
//        }
//        return filters;
//    }

    @Override
    public void onStartup(ServletContext servletContext)
            throws ServletException {
        servletContext.addFilter("CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8"))
                .addMappingForUrlPatterns(null, false, "/*");

        FilterRegistration.Dynamic compressingFilter = servletContext.addFilter("CompressingFilter", new CompressingFilter());
        compressingFilter.addMappingForUrlPatterns(null, false, "/*");
        compressingFilter.setInitParameter("includeContentTypes","text/json,text/plain,text/html,text/xml");
        //添加监听，为了关闭socketIOServer
        servletContext.addListener(ServletContextListenerImpl.class);
        super.onStartup(servletContext);
    }


//    @Override
//    public void onStartup(ServletContext servletContext) throws ServletException {
//        super.onStartup(servletContext);
//
//        registerDispatcherServlet(servletContext);
//    }

//    @Override
//    protected WebApplicationContext createRootApplicationContext() {
//        ConfigurableApplicationContext applicationContext = (ConfigurableApplicationContext) super.createRootApplicationContext();
//
//        return super.createRootApplicationContext();
//    }
}
